package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.canteenMenu.SelectCanteenMenuModel;
import com.dcy.znoa.controller.publiz.model.canteenMenu.SelectCanteenMenuOneDayModel;
import com.dcy.znoa.dao.publiz.CanteenMenuDao;
import com.dcy.znoa.domain.publiz.CanteenMenu;
import com.dcy.znoa.domain.publiz.CanteenMenuVo;
import com.dcy.znoa.service.publiz.CanteenMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CanteenMenuServiceImpl implements CanteenMenuService {

    @Autowired
    CanteenMenuDao canteenMenuDao;

    @Override
    public Integer insert(CanteenMenu var1) {
        return canteenMenuDao.insert(var1);
    }

    @Override
    public Integer updateById(CanteenMenu var1) {
        return canteenMenuDao.updateById(var1);
    }

    @Override
    public Integer deleteById(Long id) throws BusinessException {
        return canteenMenuDao.deleteById(id);
    }

    @Override
    public JsonForList selectList(SelectCanteenMenuModel model) {
        Page page = new Page(model.getPageNum(), model.getPageSize());
        EntityWrapper ew = new EntityWrapper();
        CanteenMenu canteenMenu = new CanteenMenu();
        ew.setEntity(canteenMenu);
        if(model.getCmType()!=null){
            ew.eq("cm_type",model.getCmType());
        }
        if(model.getCompId()!=null){
            ew.eq("comp_id",model.getCompId());
        }
        if(model.getParkId()!=null){
            ew.eq("park_id",model.getParkId());
        }
        if(model.getCanteenId()!=null){
            ew.eq("canteen_id",model.getCanteenId());
        }
        if(StringUtils.isNotBlank(model.getCmName())){
            ew.like("cm_name",model.getCmName());
        }
        List<CanteenMenu> list = canteenMenuDao.selectMapsPage(page,ew);
//        for (News news:list) {
//            news.setStoreFileList(storeFileService.selectStoreFileByEntityId(news.getNewsId(),"news",null));
//        }
        return new JsonForList(list,page.getTotal());
    }

    @Override
    public JsonCommon selectOneDay(SelectCanteenMenuOneDayModel model) {
        EntityWrapper ew = new EntityWrapper();
        CanteenMenu cmQuery = new CanteenMenu();
        ew.setEntity(cmQuery);
        Long canteenId = model.getCanteenId();
        ew.eq("canteen_id",canteenId);
        ew.eq("cm_time",model.getCmTime());
//        ew.eq("valid_flag",1);//
        List<CanteenMenu> list = canteenMenuDao.selectList(ew);
        CanteenMenuVo canteenMenuVo = new CanteenMenuVo();
        List<CanteenMenu> listMorning = new ArrayList<CanteenMenu>();
        List<CanteenMenu> listAfternoon = new ArrayList<CanteenMenu>();
        List<CanteenMenu> listEvening = new ArrayList<CanteenMenu>();
        for(CanteenMenu canteenMenu : list ){
            Integer cmType = canteenMenu.getCmType();
            if(cmType!=null){
                switch (cmType){
                    case 1:
                        listMorning.add(canteenMenu);
                        break;
                    case 2:
                        listAfternoon.add(canteenMenu);
                        break;
                    case 3:
                        listEvening.add(canteenMenu);
                        break;
                    default:
                        break;
                }
            }
        }
        canteenMenuVo.setCanteenId(canteenId);
        canteenMenuVo.setListMorning(listMorning);
        canteenMenuVo.setListAfternoon(listAfternoon);
        canteenMenuVo.setListEvening(listEvening);
        return new JsonCommon(canteenMenuVo);
    }
}
